var express = require('express')
var app = express()
app.set('port', process.env.PORT || 3000)

//设置handlebars视图引擎
var handlebars = require('express3-handlebars').create({defaultLayout: 'main'})
app.engine('handlebars', handlebars.engine)
app.set('view engine', 'handlebars')

app.get('/', function (req,res) {
    res.render('home')
})

//静态资源
app.use(express.static(__dirname + '/public'))

app.get('/about', function (req, res) {
    var fortunes = [
        'Conquer your fears or they will conquer you.',
        'Rivers need springs.',
        'Do not fear what you don\'t know.'
    ]
    var randomFortune = fortunes[Math.floor(Math.random()*fortunes.length)]
    res.render('about', {fortune: randomFortune})
})

// catch-all 处理器
app.use(function (req, res) {
    res.status(404)
    res.render('404')
})

// 500 错误处理器
app.use(function (err, req, res, next) {
    console.error(err.stack)
    res.status(500)
    res.render('500')
})

app.listen(app.get('port'), function () {
    console.log('Express started on http://localhost:' + app.get('port') + ';press Ctrl-C to terminate.');
})
